<template>
  <div class="app-container">

    <!-- 查询和其他操作 -->
    <el-form :inline="true" class="demo-form-inline filter">
       <el-form-item label="设定日期">
         <el-date-picker
          v-model="alarmdate"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"/>
        </el-form-item>
      <!-- <div> -->
      <!-- <el-form-item label="工作说明">
        <el-input v-model="listQuery.fworkmemo" clearable class="filter-item" placeholder="请输入工作说明"/>
      </el-form-item>
      <el-form-item class="searchdate" label="设定日期">
       <el-date-picker v-model="listQuery.fplandate" clearable class="filter-item" type="date" placeholder="请输入设定日期"></el-date-picker>
      </el-form-item>
      <el-form-item label="设定人员">
        <el-input v-model="listQuery.fplanuser" clearable class="filter-item" placeholder="请输入设定人员"/>
      </el-form-item> -->
      <!--<el-input v-model="listQuery.fsiteNo" clearable class="filter-item" style="width: 200px;" placeholder="请输入所属学校"/>-->
      <!--<el-input v-model="listQuery.fpointNo" clearable class="filter-item" style="width: 200px;" placeholder="请输入实验室编号"/>-->
      <!-- <el-form-item label="实验室">
        <div style="width: 220px;display:flex;justify-content:flex-start;align-items:center">
          <el-select style="display: inline-block" clearable filterable v-model="listQuery.fpointNo" class="filter-item" placeholder="请选择实验室">
            <el-option
              v-for="item in getListfpointNo"
              :key="item.value"
              :label="item.label"
              :value="item.value"/>
          </el-select>
          <i class="el-icon-search selectI" @click="searchDialoglab('search')"/>
        </div>
      </el-form-item> -->
      <!-- </div> -->
      <!-- <div> -->
      <el-form-item>
        <el-button round class="filter-item" :style="{background:buttonColor.lookup,color:buttonColor.color}" @click="handleFilter">查找</el-button><!--v-permission="[':planHead:read']" -->
      </el-form-item>
      <!--<el-button class="filter-item" :style="{background:buttonColor.add,color:buttonColor.color}" icon="el-icon-edit" @click="handleCreate">添加</el-button>--><!--v-permission="[':planHead:create']" -->
      <!-- </div> -->
    </el-form>

    <!-- 查询结果 -->
    <el-table :height="buttonColor.clientHeightTwo" v-loading="listLoading" :data="list" stripe element-loading-text="正在查询中。。。" border fit highlight-current-row :header-cell-style="tableColor">
      <el-table-column type="expand">
        <template slot-scope="scope">
          <el-table :data="scope.row.details3" row-key="id" border fit highlight-current-row :header-cell-style="tableColorDetail">
            <el-table-column :width="buttonColor.line" align="center" label="序" prop="fentryid"/>
            <el-table-column align="left" label="工作单号" prop="fbillno" />
            <el-table-column align="left" label="执行次数" prop="ftime" />
            <el-table-column align="left" label="固定资产码" prop="fequipmentbarcode" />
            <el-table-column align="left" label="物料编号" prop="fgoodno" />
            <el-table-column align="left" label="速度档位" prop="fspeed" />
            <el-table-column align="left" label="单位" prop="funit" />
            <el-table-column align="left" label="设定重量" prop="fsetweight" />
            <el-table-column align="left" label="实际重量" prop="fweight" />
            <el-table-column :width="buttonColor.date" :formatter="datetimeFormat" align="left" label="开始时间" prop="fbegtime" />
            <el-table-column :width="buttonColor.date" :formatter="datetimeFormat" align="left" label="结束时间" prop="fendtime" />
          </el-table>
        </template>
      </el-table-column>
      <el-table-column :index="indexMethod" :width="buttonColor.line" align="center" label="序" type="index" />
      <el-table-column align="left" label="工作说明" prop="fworkmemo" />
      <el-table-column :formatter="datetimeFormat2" align="left" label="设定日期" prop="fplandate" />
      <el-table-column :formatter="formatforduser" align="left" label="设定人员" prop="fplanuser" />
      <el-table-column :formatter="formatfsiteNo" align="left" label="所属学校" prop="fsiteNo" />
      <el-table-column :formatter="formatfpointNo" align="left" label="实验室" prop="fpointNo" />
      <el-table-column align="left" label="备注" prop="fexplanation" />
      <el-table-column :width="buttonColor.date" :formatter="datetimeFormat2" align="left" label="修改时间" prop="fupddate" />
      <el-table-column :width="buttonColor.date" :formatter="datetimeFormat2" align="left" label="确认时间" prop="fcheckdate" />
      <el-table-column :width="buttonColor.user" :formatter="formatforduser2" align="left" label="确认员" prop="fcheckuser" />
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />

    <!-- 添加或修改对话框 -->
    <!--<el-dialog :close-on-click-modal="false" :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">-->
    <el-dialog
      :close-on-click-modal="false"
      :visible.sync="dialogFormVisible"
      width="auto"
      top="2vh"
    >
      <template slot="title">
        <div :style="titleColor">{{ textMap[dialogStatus] }}</div>
      </template>
      <el-form
        ref="dataForm"
        :rules="rules"
        :model="dataForm"
        status-icon
        label-position="left"
        label-width="auto"
      >
        <!--<el-form ref="dataForm" :rules="rules" :model="dataForm" status-icon label-position="left" label-width="100px" style="width: 400px; margin-left:50px;">-->
        <el-row>
          <el-col :span="12">
            <el-form-item label="工作单号" prop="fbillno">
              <el-input v-model="dataForm.fbillno"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="工作说明" prop="fworkmemo">
              <el-input v-model="dataForm.fworkmemo"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="所属学校" prop="fsiteNo">
              <!--<el-input v-model="dataForm.fsiteNo"/>-->
              <el-select :disabled='true' @change="fsiteNoChange" v-model="dataForm.fsiteNo" clearable filterable class="filter-item" placeholder="请选择客户名称">
                <el-option
                  v-for="item in getListfsiteNo"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              v-if="this.$store.state.user.fsiteLevel==3"
              label="实验室"
              prop="fpointNo"
            >
              <div style="display:flex;justify-content:flex-start;align-items:center">
                <el-select style="display: inline-block" clearable filterable v-model="dataForm.fpointNo" class="filter-item" placeholder="请选择实验室">
                  <el-option
                    v-for="item in getListfpointNo"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"/>
                </el-select>
                <i style="display: inline-block;color:#409EFF;margin-left:3px" class="el-icon-search" @click="searchDialoglab('add')"/>
              </div>
            </el-form-item>
            <el-form-item
              v-else
              label="实验室"
              prop="fpointNo"
            >
              <div style="display:flex;justify-content:flex-start;align-items:center">
                <el-select style="display: inline-block" clearable filterable v-model="dataForm.fpointNo" class="filter-item" placeholder="请选择实验室">
                  <el-option
                    v-for="item in getListfpointNoAreaAdd"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"/>
                </el-select>
                <i style="display: inline-block;color:#409EFF;margin-left:3px" class="el-icon-search" @click="searchDialoglab('add')"/>
              </div>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="设定日期" prop="fplandate">
              <el-date-picker v-model="dataForm.fplandate" type="datetime"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="备注" prop="fexplanation">
              <el-input v-model="dataForm.fexplanation"/>
            </el-form-item>
          </el-col>
        </el-row>
        <!--<el-row>
          <el-col :span="12">
            <el-form-item label="设定人员" prop="fplanuser">
              <el-input v-model="dataForm.fplanuser"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            &lt;!&ndash;<el-form-item label="操作员" prop="foperator">
              <el-input v-model="dataForm.foperator"/>
            </el-form-item>
            <el-form-item label="修改时间" prop="fupddate">
              <el-date-picker v-model="dataForm.fupddate" type="datetime"/>
            </el-form-item>&ndash;&gt;
            <el-form-item label="确认时间" prop="fcheckdate">
              <el-date-picker v-model="dataForm.fcheckdate" type="datetime"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="确认员" prop="fcheckuser">
              <el-input v-model="dataForm.fcheckuser"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="单据状态" prop="fbillstate">
              <el-input v-model.number="dataForm.fbillstate"/>
            </el-form-item>
          </el-col>
        </el-row>-->
      </el-form>
      <el-tabs type="border-card">
        <el-tab-pane label="物料列表">
          <div class="filter-container">
            <el-button class="filter-item" :style="{background:buttonColor.lookup,color:buttonColor.color}" icon="el-icon-edit" @click="handleUpdate1()">添加物料</el-button><!-- v-permission="['equipment:eqtDict:create']"-->
            <el-table v-loading="listLoading" :data="dataForm.details" element-loading-text="正在查询中。。。" border fit highlight-current-row>

              <el-table-column :index="indexMethod" align="center" label="序" type="index"/>
              <!--<el-table-column align="right" label="序号" prop="fentryid" />-->
              <el-table-column align="right" label="物料编号" prop="fgoodno" />
              <el-table-column
                :formatter="formatfgoodno"
                align="center"
                label="物料名称"
                prop="fgoodname"
              />
              <el-table-column align="right" label="速度档位" prop="fspeed" />
              <el-table-column align="right" label="设定重量" prop="fsetweight" />
              <el-table-column align="right" label="单位" prop="funit" >
                <template slot-scope="scope">
                  <el-tag>{{ scope.row.funit | siteTypeFilter }}</el-tag>
                </template>
              </el-table-column>
              <el-table-column align="center" width="140" label="操作" class-name="small-padding">
                <template slot-scope="scope">
                  <el-button :style="{background:buttonColor.delete,color:buttonColor.color}" size="mini" @click="handleDelete1(scope.row)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
          </div>
        </el-tab-pane>
        <el-tab-pane label="设备列表">
          <div class="filter-container">
            <el-button class="filter-item" :style="{background:buttonColor.add,color:buttonColor.color}" icon="el-icon-edit" @click="handleUpdate2(dataForm.fpointNo)">添加设备</el-button><!-- v-permission="['equipment:eqtDict:create']"-->
            <el-table v-loading="listLoading" :data="dataForm.details2" element-loading-text="正在查询中。。。" border fit highlight-current-row>

              <el-table-column :index="indexMethod" align="center" label="序" type="index"/>
              <el-table-column align="right" label="固定资产码" prop="fequipmentbarcode" />
              <el-table-column :formatter="formatfpduser" align="right" label="设备标签" prop="fequipmentbarcode" />
              <el-table-column align="center" width="140" label="操作" class-name="small-padding">
                <template slot-scope="scope">
                  <el-button :style="{background:buttonColor.delete,color:buttonColor.color}" size="mini" @click="handleDelete2(scope.row)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
          </div>
        </el-tab-pane>
      </el-tabs>
      <div slot="footer" class="dialog-footer">
        <el-button :style="{background:buttonColor.cancel,color:buttonColor.colorone}" @click="cancel()">取消</el-button>
        <el-button v-if="dialogStatus=='create'" :style="{background:buttonColor.confirm,color:buttonColor.color}" @click="createData">确定</el-button>
        <el-button v-else :style="{background:buttonColor.confirm,color:buttonColor.color}" @click="updateData">确定</el-button>
      </div>
    </el-dialog>
    <el-dialog :close-on-click-modal="false" :title="textMap[dialogStatus1]" :visible.sync="dialogFormVisible1">
      <el-form ref="dataForm1" :rules="rules" :model="dataForm1" status-icon label-position="left" label-width="100px" style="width: 400px; margin-left:150px;">
        <!--<el-form-item label="序号" prop="fentryid">
          <el-input v-model.number="dataForm1.fentryid"/>
        </el-form-item>-->
        <el-form-item label="物料编号" prop="fgoodno">
          <div
            style="
                  display: flex;
                  justify-content: flex-start;
                  align-items: center;
                "
          >
            <el-input
              v-model="dataForm1.fgoodno"
              style="display: inline-block"
            />
            <i
              style="
                    display: inline-block;
                    color: #409eff;
                    margin-left: 3px;
                  "
              class="el-icon-search"
              @click="searchDialogMaterial()"
            />
          </div>
        </el-form-item>
        <el-form-item label="物料名称" prop="fgoodname">
          <el-input disabled v-model="dataForm1.fgoodname" />
        </el-form-item>
        <el-form-item label="速度档位" prop="fspeed">
          <el-input v-model.number="dataForm1.fspeed"/>
        </el-form-item>
        <el-form-item label="单位" prop="funit">
          <el-input v-model.number="dataForm1.funit"/>
        </el-form-item>
        <el-form-item label="设定重量" prop="fsetweight">
          <el-input v-model="dataForm1.fsetweight"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :style="{background:buttonColor.cancel,color:buttonColor.colorone}" @click="dialogFormVisible1 = false">取消</el-button>
        <el-button :style="{background:buttonColor.confirm,color:buttonColor.color}" @click="addData">确定</el-button>
      </div>
    </el-dialog>
    <el-dialog :close-on-click-modal="false" :title="textMap[dialogStatus2]" :visible.sync="dialogFormVisible2">
      <el-form ref="dataForm2" :rules="rules" :model="dataForm2" status-icon label-position="left" label-width="110px" style="width: 400px; margin-left:50px;">
        <el-table ref="multipleTable" :data="details2" tooltip-effect="dark" style="width: 100%" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55"/>
          <el-table-column align="right" label="固定资产码" prop="fequipmentbarcode" />
          <el-table-column align="right" label="设备标签" prop="fequipmenttag" />
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :style="{background:buttonColor.cancel,color:buttonColor.colorone}" @click="dialogFormVisible2 = false">取消</el-button>
        <el-button :style="{background:buttonColor.confirm,color:buttonColor.color}" @click="addData2">确定</el-button>
      </div>
    </el-dialog>
    <labDialog :searchVisiblelab.sync="searchVisiblelab" :fsiteNo.sync="labDialogFsiteNo" :tableColor="tableColor" @labDialog="labDialogValue" />
    <material
      :materialIdVisible.sync="materialIdVisible"
      :fsiteNo.sync="materialFsiteNo"
      :tableColor="tableColor"
      @materialDialog="materialDialogValue"
    />
  </div>
</template>

<style>
  .button-margin {
    margin : 2px 0px;
  }
  .el-button+.el-button {
    margin-left: 1px;
  }
</style>

<script>
  import { listplanHead, createplanHead, updateplanHead, deleteplanHead, listqualityEqtByFpointNo } from '@/api/planHead'
  import { fsiteOptionsByLevel,getListArea, getFequipmentbarcode,getPointOptions,getFbillno} from '@/api/area'
  import { checkBiz } from '@/api/area'
  import { tableColor, titleColor,buttonColor,tableColorDetail } from '@/filters/index'
  import LabDialog from '@/components/LabDialog'
  import { listRoom } from '@/api/room'
  import Material from "@/components/Material";
  import Pagination from '@/components/Pagination'
  import moment from 'moment'

  const siteTypeMap = {
    0: 'g',
    1: 'ml'
  }

  export default {
    name: 'ITcPlanHead3',
    components: { Pagination, LabDialog, Material },
    filters: {
      siteTypeFilter(type) {
        return siteTypeMap[type]
      }
    },
    data() {
      return {
        alarmdate: null,
        tableColorDetail,
        buttonColor,
        getFsiteNo:null,
        labFlag:null,
        labDialogFsiteNo:null,
        selectListlab:[],
        searchVisiblelab: false,
        materialIdVisible: false,
        materialFsiteNo: null,
        searchListlab: [],
        goodAllList: [],
        listLoadingSearchlab: true,
        totalSearchlab: 0,
        listQuerySearchlab: {
          page: 1,
          limit: 20,
          fpointName: undefined,
          sort: 'create_time',
          order: 'desc'
        },

        searchFlaglab:'',
        siteTypeMap,
        getListforduser:null,
        fstateOptions: null,
        getListfclassstno: null,
        getListfequipmenttype: null,
        getListfsiteNo: null,
        getListfpointNo: null,
        getListfpointNoQuery: null,
        getListfpointNoAll: null,
        list: null,
        total: 0,
        listLoading: true,
        listQuery: {
          page: 1,
          limit: 20,
          fworkmemo: undefined,
          fplandate: undefined,
          fplanuser: this.$store.state.user.id,
          fsiteNo: undefined,
          fpointNo: undefined,
          fexplanation: undefined,
          foperator: undefined,
          fupddate: undefined,
          fcheckdate: undefined,
          fcheckuser: undefined,
          fbillstate: undefined,
          sort: 'create_time',
          order: 'desc'
        },
        searchListAll: [],
        searchList: [],
        searchVisible: false,
        totalSearch: 0,
        listLoadingSearch: true,
        listQuerySearch: {
          page: 1,
          limit: 20,
          fequipmentno: undefined,
          sort: 'create_time',
          order: 'desc'
        },
        dataForm: {
          fbillno: undefined,
          fworkmemo: undefined,
          fplandate: undefined,
          fplanuser: undefined,
          fsiteNo: undefined,
          fpointNo: undefined,
          fexplanation: undefined,
          foperator: undefined,
          fupddate: undefined,
          fcheckdate: undefined,
          fcheckuser: undefined,
          fbillstate: undefined,
          id: undefined,
          details: [],
          details2: []
        },
        tableColor: tableColor,
        titleColor: titleColor,
        details2: null,
        dataForm1: {},
        dataForm2: {},
        dialogFormVisible: false,
        dialogFormVisible1: false,
        dialogFormVisible2: false,
        dialogStatus: '',
        dialogStatus1: '',
        dialogStatus2: '',
        detailsTmp: [],
        detailsTmp2: [],
        multipleSelection: [],
        textMap: {
          update: '编辑',
          create: '创建'
        },
        rules: {
          fworkmemo: [
            { required: true, message: '工作说明不能为空' },
          ],
          fplandate: [
            { required: true, message: '设定日期不能为空' },
          ],
          fplanuser: [
            { required: true, message: '设定人员不能为空' },
          ],
          fsiteNo: [
            { required: true, message: '所属学校不能为空' },
          ],
          foperator: [
            { required: true, message: '操作员不能为空' },
          ],
          fupddate: [
            { required: true, message: '修改时间不能为空' },
          ],
          fbillstate: [
            { type: 'number', message: '单据状态必须为数字值' },
          ],
        }
      }
    },
    created() {
      this.getList()
      this.alarmdate = [new Date(), new Date()]
      //学校
      fsiteOptionsByLevel(3)
        .then(response => {
          this.getListfsiteNo = response.data.data.list
        })
        .catch(() => {
          this.getListfsiteNo = []
        })
      // 物料
      getListArea({ flag: 31 })
        .then((response) => {
          this.goodAllList = response.data.data.list;
        })
        .catch(() => {
          this.goodAllList = [];
        });

      getListArea({flag:13})
        .then(response => {
          this.getListforduser = response.data.data.list
        })
        .catch(() => {
          this.getListforduser = []
        })
      if(this.$store.state.user.fsiteLevel==3){
        this.listQuery.fsiteNo = this.$store.state.user.fsiteNo;
        // 实验室
        getListArea({ flag: 10,arg1:this.$store.state.user.fsiteNo })
          .then(response => {
            this.getListfpointNo = response.data.data.list
          })
          .catch(() => {
            this.getListfpointNo = []
          })
      }else{
        // 实验室
        getListArea({ flag: 10 })
          .then(response => {
            this.getListfpointNo = response.data.data.list
          })
          .catch(() => {
            this.getListfpointNo = []
          })
      }

    },
    methods: {
      datetimeFormat(row, column) {
        const date = row[column.property]
        if (date === undefined || date === null) {
          return ''
        }
        return moment(date).format('HH:mm:ss')
      },
      datetimeFormat2(row, column) {
        const date = row[column.property]
        if (date === undefined || date === null) {
          return ''
        }
        return moment(date).format('YYYY-MM-DD')
      },
      formatfsiteNo: function(row, column) {
        if (this.getListfsiteNo) {
          for (let i = 0; i < this.getListfsiteNo.length; i++) {
            if (row.fsiteNo === this.getListfsiteNo[i].value) {
              return this.getListfsiteNo[i].label
            }
          }
          return ''
        } else {
          return ''
        }
      },
      // 实验室
      formatfpointNo: function(row, column) {
        if (this.getListfpointNo) {
          for (let i = 0; i < this.getListfpointNo.length; i++) {
            if (row.fpointNo === this.getListfpointNo[i].value) {
              return this.getListfpointNo[i].label
            }
          }
          return ''
        } else {
          return ''
        }
      },

      // 人员
      formatforduser: function(row, column) {
        if(this.getListforduser){
          for (let i = 0; i < this.getListforduser.length; i++) {
            if (row.fplanuser == this.getListforduser[i].value) {
              return this.getListforduser[i].label
            }
          }
          return ''
        }else{
          return ''
        }
      },

      // 人员
      formatforduser2: function(row, column) {
        if(this.getListforduser){
          for (let i = 0; i < this.getListforduser.length; i++) {
            if (row.fcheckuser == this.getListforduser[i].value) {
              return this.getListforduser[i].label
            }
          }
          return ''
        }else{
          return ''
        }
      },
      // 设备标签
      formatfpduser: function(row, column) {
        if(this.details2){
          debugger
          for (let i = 0; i < this.details2.length; i++) {
            if (row.fequipmentbarcode == this.details2[i].fequipmentbarcode) {
              return this.details2[i].fequipmenttag
            }
          }
          return ''
        }else{
          return ''
        }
      },
      // 搜索地点弹出框
      searchDialoglab(value) {
        this.labFlag = value
        if(value === 'add'){
          this.labDialogFsiteNo = this.dataForm.fsiteNo
        }else{
          this.labDialogFsiteNo = this.listQuery.fsiteNo
        }
        this.searchVisiblelab = true
      },

      // 地点弹出框返回值
      labDialogValue(value){
        if(this.labFlag==='add'){
          this.dataForm.fpointNo = value.lab.fpointNo
        }else{
          this.listQuery.fpointNo = value.lab.fpointNo
        }
      },

      fsiteNoChange(data){
        this.dataForm.fpointNo=''
        this.searchListlab=[]
        this.getListfpointNo = []
        if(data){
          this.getFsiteNo=data
          this.getSearchListlab(data)
          getPointOptions(data)
            .then(response => {
              this.getListfpointNo = response.data.data.list
            })
            .catch(() => {
              this.getListfpointNo = []
            })
        }
      },
      fsiteNoQueChange(data){
        this.listQuery.fpointNo=''
        this.searchListlab=[]
        this.getListfpointNoQuery = []

        if(data){
          this.getFsiteNo=data
          getPointOptions(data)
            .then(response => {
              this.getListfpointNoQuery = response.data.data.list
            })
            .catch(() => {
              this.getListfpointNoQuery = []
            })
        }

      },
      // 地点获取搜索列表
      getSearchListlab(data) {
        this.listLoadingSearchlab = true
        if(data){
          const temp = {
            fsiteNo:data,
            fpointFlag:4,
            page:this.listQuerySearchlab.page,
            limit:this.listQuerySearchlab.limit,
            sort: 'create_time',
            order: 'desc',
            fpointName:this.listQuerySearchlab.fpointName,
          }
          listRoom(temp)
            .then(response => {
              this.searchListlab = response.data.data.list
              this.totalSearchlab = response.data.data.total
              this.listLoadingSearchlab = false
            })
            .catch(() => {
              this.searchListlab = []
              this.totalSearchlab = 0
              this.listLoadingSearchlab = false
            })
        }else{
          this.searchListlab = []
        }
      },
      // 物料弹出框
      searchDialogMaterial() {
        this.materialIdVisible = true;
        this.materialFsiteNo = this.dataForm.fsiteNo;
      },

      // 物料弹出框
      materialDialogValue(value) {
        this.dataForm1.fgoodno = value.data.fgoodno;
        this.dataForm1.fgoodname = value.data.fgoodname;
        // this.dataForm1.fbuyprice = value.data.fbuyprice;
        // if (value.data.fbalqty) {
        //   this.dataForm1.fbalqty = value.data.fbalqty;
        // } else {
        //   this.dataForm1.fbalqty = 0;
        // }
        // this.dataForm1.frfidbarcode = 0;
        // this.dataForm1.fuseqty = 0;
        // this.dataForm1.fboxbarcode = 0;
        // this.dataForm1.fcabbarcode = 0;
      },
      // 物料
      formatfgoodno: function (row, column) {
        if (this.goodAllList.length > 0) {
          for (let i = 0; i < this.goodAllList.length; i++) {
            if (row.fgoodno === this.goodAllList[i].value) {
              return this.goodAllList[i].label;
            }
          }
          return "";
        } else {
          return "";
        }
      },
      // 地点获取搜索列表
      getSearchListlabPage(data) {
        this.listLoadingSearchlab = true
        let queryFsiteNo
        if(data){
          if(this.searchFlaglab==='add'){
            // queryFsiteNo = this.dataForm.fsiteNo
            queryFsiteNo = this.$store.state.user.fsiteNo
          }else{
            // queryFsiteNo = this.listQuery.fsiteNo
            queryFsiteNo = this.$store.state.user.fsiteNo
          }
          const temp = {
            fsiteNo:queryFsiteNo,
            fpointFlag:4,
            page:this.listQuerySearchlab.page,
            limit:this.listQuerySearchlab.limit,
            sort: 'create_time',
            order: 'desc',
            fpointName:this.listQuerySearchlab.fpointName,
          }
          listRoom(temp)
            .then(response => {
              this.searchListlab = response.data.data.list
              this.totalSearchlab = response.data.data.total
              this.listLoadingSearchlab = false
            })
            .catch(() => {
              this.searchListlab = []
              this.totalSearchlab = 0
              this.listLoadingSearchlab = false
            })
        }else{
          this.searchListlab = []
        }
      },


      // 地点获取搜索列表
      searchFilterlab() {
        this.listQuerySearchlab.page = 1
        this.getSearchListlab(this.getFsiteNo)
      },
      // 地点获取搜索列表
      searchDatalab(row) {
        this.searchVisiblelab = false
        this.listQuerySearchlab.page = 1
        if(this.searchFlaglab==='add'){
          this.dataForm.fpointNo = row.fpointNo
        }else{
          this.listQuery.fpointNo = row.fpointNo
        }
      },

      // 搜索地点获取焦点
      fpointNoFocus(){
        if(!this.listQuery.fsiteNo){
          this.getListfpointNoQuery = []
        }
      },
      formatDate: function(date) {
      if(typeof(date)==='object'){
         var ym
      var y = date.getFullYear()
      var m = date.getMonth() + 1
      var d = date.getDate()
      if (m < 10) {
        m = '0' + m
      }
      if (d < 10) {
        d = '0' + d
      }
      ym = '' + y + m + d
      return ym
      }else{
        return date
      }
    },

      getList() {
        this.listQuery.ftakedateStart = this.alarmdate == null ? null : this.formatDate(this.alarmdate[0])
        this.listQuery.ftakedateEnd = this.alarmdate == null ? null : this.formatDate(this.alarmdate[1])
        this.listLoading = true
        this.listQuery.fbillstate = 3
        listplanHead(this.listQuery)
          .then(response => {
            this.list = response.data.data.list
            this.total = response.data.data.total
            this.listLoading = false
          })
          .catch(() => {
            this.list = []
            this.total = 0
            this.listLoading = false
          })
      },
      handleFilter() {
        this.listQuery.page = 1
        this.getList()
      },
      resetForm() {
        this.dataForm = {
          fworkmemo: undefined,
          fplandate: undefined,
          fplanuser: undefined,
          fsiteNo: undefined,
          fpointNo: undefined,
          fexplanation: undefined,
          foperator: undefined,
          fupddate: undefined,
          fcheckdate: undefined,
          fcheckuser: undefined,
          fbillstate: undefined,
          id: undefined,
          details: [],
          details2: []
        }
      },
      handleCreate() {
        this.resetForm()
        this.dialogStatus = 'create'
        this.dataForm.fsiteNo = this.$store.state.user.fsiteNo
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
        getFbillno({ flag: 2027 })
          .then((response) => {
            if (response) {
              this.dataForm.fbillno = response.data.data
              this.dialogFormVisible = true
            }
          })
          .catch(() => {
            this.dataForm.fbillno = ''
          })
      },
      createData() {
        this.$refs['dataForm'].validate(valid => {
          if (valid) {
            this.dataForm.fbillstate=1
            createplanHead(this.dataForm)
              .then(response => {
                this.list.unshift(response.data.data)
                this.dialogFormVisible = false
                this.$notify.success({
                  title: '成功',
                  message: '添加智量仪工作计划主表成功'
                })
              })
              .catch(response => {
                this.$notify.error({
                  title: '失败',
                  message: response
                })
              })
          }
        })
      },
      handleUpdate(row) {
        this.dataForm = Object.assign({}, row)
        this.dialogStatus = 'update'
        this.dialogFormVisible = true
        this.detailsTmp = []
        row.details.forEach(v => {
          this.detailsTmp.push(v)
        })
        this.detailsTmp2 = []
        row.details2.forEach(v => {
          this.detailsTmp2.push(v)
        })
        this.getSearchListlab(row.fsiteNo)
        getPointOptions(row.fsiteNo)
          .then(response => {
            this.getListfpointNo = response.data.data.list
          })
          .catch(() => {
            this.getListfpointNo = []
          })
        this.dataForm.fsiteNo = this.$store.state.user.fsiteNo
        listqualityEqtByFpointNo(row.fpointNo)
          .then(response => {
            this.details2 = response.data.data.list
          })
          .catch(() => {
            this.details2 = []
          })
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      },
      updateData() {
        this.$refs['dataForm'].validate(valid => {
          if (valid) {
            updateplanHead(this.dataForm, this.$route.query.id)
              .then(() => {
                for (const v of this.list) {
                  if (v.id === this.dataForm.id) {
                    const index = this.list.indexOf(v)
                    this.list.splice(index, 1, this.dataForm)
                    break
                  }
                }
                this.dialogFormVisible = false
                this.$notify.success({
                  title: '成功',
                  message: '更新智量仪工作计划主表成功'
                })
              })
              .catch(response => {
                this.$notify.error({
                  title: '失败',
                  message: response
                })
              })
          }
        })
      },
      handleDelete(row) {
        this.$confirm('是否要删除智量仪工作计划主表', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          deleteplanHead(row)
            .then(response => {
              this.$notify.success({
                title: '成功',
                message: '删除智量仪工作计划主表成功'
              })
              const index = this.list.indexOf(row)
              this.list.splice(index, 1)
            })
            .catch(response => {
              this.$notify.error({
                title: '失败',
                message: response
              })
            })
        })
      },
      audit(row) {
        this.$confirm('是否确定', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.dataForm = Object.assign({}, row)
          checkBiz({ pBillNo: this.dataForm.fbillno ,pBillState:2 ,pBizType: 'i_tc_plan_head', pUser:this.$store.state.user.id })
            .then(() => {
              for (const v of this.list) {
                if (v.fbillno === this.dataForm.fbillno) {
                  this.dataForm.fbillstate = 2
                  const index = this.list.indexOf(v)
                  this.list.splice(index, 1, this.dataForm)
                  break
                }
              }
              this.$notify.success({
                title: '成功',
                message: '确认成功'
              })
            })
            .catch((response) => {
              this.$notify.error({
                title: '失败',
                message: response.data.data
              })
            })
        })
      },
      addData() {
        this.dialogFormVisible1 = false;
        // for (var i in this.dataForm.actions) {
        //   var eqt = this.dataForm.actions[i]
        //   if (eqt.faction === this.dataForm1.faction) { //重复不添加
        //     return
        //   }
        // }
        this.dataForm.details.push(this.dataForm1);
        // this.dataForm.actions.push(
        //   { fairitem: this.dataForm1.fairitem.value, fairitemname: this.dataForm1.fairitem.label, fwhere: this.sTcSceneItem.fwhere, fwhere2: this.sTcSceneItem.fwhere2, fvalue: this.sTcSceneItem.fvalue, fvalue2: this.sTcSceneItem.fvalue2 })
      },

      addData2() {
        this.dialogFormVisible2 = false
        for (var i in this.multipleSelection) {
          var atsUser = this.multipleSelection[i]
          var canAdd = true
          for (var j in this.dataForm.details2) {
            if (atsUser.fequipmentbarcode + '' === this.dataForm.details2[j].fequipmentbarcode) {
              canAdd = false
              break
            }
          }
          if (canAdd) {
            atsUser.fbillno = this.dataForm.fbillno
            this.dataForm.details2.push(atsUser)
            // this.multiple.push(atsUser)
          }
        }
      },
      cancel() {
        this.dialogFormVisible = false;
        this.list.forEach((v) => {
          if (v.fbillno === this.dataForm.fbillno) {
            // this.dataForm = this.listTmp;
            this.dataForm.details = this.detailsTmp;
            this.dataForm.details2 = this.detailsTmp2;
            const index = this.list.indexOf(v);
            this.list.splice(index, 1, this.dataForm);
            return;
          }
        });
      },
      handleUpdate1() {
        this.dialogStatus1 = "update";
        this.dialogFormVisible1 = true;
        // this.multipleSelection = []
        this.dataForm1 = {};
        this.$nextTick(() => {
          this.$refs["dataForm1"].clearValidate();
        });
      },
      handleUpdate2(fpointNo) { //todo
        if (fpointNo == null) {
          this.$notify.info({
            title: '',
            message: '请先选择实验室'
          })
          return
        }
        listqualityEqtByFpointNo(fpointNo)
          .then(response => {
            this.details2 = response.data.data.list
          })
          .catch(() => {
            this.details2 = []
          })
        this.dialogStatus2 = 'update'
        this.dialogFormVisible2 = true
        this.multipleSelection = []
        this.$nextTick(() => {
          this.$refs['dataForm2'].clearValidate()
        })
      },
      handleDelete1(row) {
        const index = this.dataForm.details.indexOf(row);
        this.dataForm.details.splice(index, 1);
        // this.dataForm.actions.pop(row)
      },
      handleDelete2(row) {
        const index = this.dataForm.details2.indexOf(row);
        this.dataForm.details2.splice(index, 1);
      },
      toggleSelection(rows) {
        if (rows) {
          rows.forEach(row => {
            this.$refs.multipleTable.toggleRowSelection(row)
          })
        } else {
          this.$refs.multipleTable.clearSelection()
        }
      },
      handleSelectionChange(val) {
        this.multipleSelection = val
      },
      indexMethod(index) {
        index = index + 1
        return index
      }
    }
  }
</script>
